package com.bear.fastrepair.mvvm.data.manager

import com.bear.fastrepair.entity.CreateManagerInfo
import com.bear.fastrepair.entity.OrderInfoPage2
import com.bear.fastrepair.entity.ResponseInfo
import com.bear.fastrepair.http.api.ManagerService
import com.bear.fastrepair.http.api.OrdersService
import com.bear.fastrepair.http.client.RetrofitManager
import com.bear.fastrepair.mvvm.data.Result
import com.bear.fastrepair.utils.ACCOUNT_TOKEN_KEY
import com.bear.fastrepair.utils.SharedPreferencesHelper
import com.bear.fastrepair.utils.URL

class ManagerDataSource {
    suspend fun addManagerUser(
        account: String,
        avatar: String,
        password: String,
        regionCode: String,
        regionText: String = "",
        roleCode: String,
        tel: String,
        userName: String,
        token: String = ""
    ): Result<ResponseInfo> {
        val createManagerInfo = CreateManagerInfo(
            account,
            avatar,
            password,
            regionCode,
            regionText,
            roleCode,
            tel,
            userName
        )
        return try {
            RetrofitManager.getServiceSetToken(URL, ManagerService::class.java, token)
                .addManager(createManagerInfo).let {
                    if (it.code == 0) {
                        Result.Success(it)
                    } else Result.Error(data = it)
                }
        } catch (e: Exception) {
            Result.Error(e)
        }
    }

    suspend fun getOrderInfoPage(
        current: Int,
        size: Int,
        clientName: String? = null,
        clientTel: String? = null,
        employeeId: String? = null,
        orderNum: String? = null,
        orderStatus: Int? = null,
        orderTotalityStatus: Int? = null,
        regionCode: String? = null,
        token: String? = null
    ): Result<OrderInfoPage2> {
        return try {
            val tokenSave = SharedPreferencesHelper.getString(ACCOUNT_TOKEN_KEY) ?: ""
            RetrofitManager.getServiceSetToken(URL, OrdersService::class.java, tokenSave)
                .getOrderInfoPageNew(
                    current,
                    size,
                    clientName,
                    clientTel,
                    employeeId,
                    orderNum,
                    orderStatus,
                    orderTotalityStatus,
                    regionCode
                ).let { if (it.code == 0) Result.Success(it) else Result.Error(data = it) }
        } catch (e: Exception) {
            Result.Error(e)
        }
    }
}